package com.IO流.OutputStream字节输出流;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
@SuppressWarnings("all")
public class Exercise01 {
    public static void main(String[] args) {
        /**
         * 将D:\JAVA_test\1.png  拷贝到  D:\JAVA_test\2.png
         */
        String srcFilePath = "D:\\JAVA_test\\srcImage.png";  ///原图片
        String destFilePath = "D:\\JAVA_test\\Targetimage.png";  ///目标地址
        FileInputStream fileInputStream = null;  ///初始化输入流
        FileOutputStream fileOutputStream = null; ///初始化输出流
        try {
            fileInputStream = new FileInputStream(srcFilePath);  ///创建输入流
            fileOutputStream = new FileOutputStream(destFilePath); ///创建输出流
            byte[] buffer = new byte[1024];  ///定义一次读取1024个字节的数组
            int len = 0;  ///存放获取到的字节长度
            while((len = fileInputStream.read(buffer)) != -1){  /// 如果读取还未结束
                fileOutputStream.write(buffer,0,len);  ///从buffer数组的0索引开始写入len个字节
            }
            System.out.println("ok~~");
        } catch (IOException e) {
            throw new RuntimeException(e);
        } finally {
            try {
                if (fileInputStream != null) {
                    fileInputStream.close();   ///关闭输入流
                }
                if (fileOutputStream != null) {
                    fileOutputStream.close();  ///关闭输出流
                }
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }

    }
}
